package com.ruyuan2020.im.x;

/**
 * 移除链表的元素
 */
public class _33 {


    public static class RemoveLinkedListElements {

        /**
         * Definition for singly-linked list.
         * public class ListNode {
         *     int val;
         *     ListNode next;
         *     ListNode() {}
         *     ListNode(int val) { this.val = val; }
         *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
         * }
         */
        class Solution {
            public ListNode removeElements(ListNode head,int val){
                ListNode dummyNode = new ListNode(-1);
                dummyNode.next=head;
                ListNode curr =head;
                ListNode prev =dummyNode;
                while (curr!=null){
                    if (curr.val==val){
                        prev.next=curr.next;
                        curr.next=null;
                    }else {
                        prev=curr;
                    }
                    curr=prev.next;

                }
                return dummyNode.next;
            }

        }




    }
    public static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }

}
